package com.sms.web.system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.sms.common.exception.SystemException;
import com.sms.web.rest.RestObject;
import com.sms.web.system.entity.SysUser;
import com.sms.web.system.service.SysUserService;
import com.sms.web.vo.LoginVo;

/**
 * @desc 登录控制器
 * @author WuJiangWu
 * @date 2018年7月24日上午00:29:53
 */
@RestController
public class LoginController {
    
    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
    
    @Autowired
    private SysUserService userService;
    
    /**
     * 提交登录
     * @param username xx
     * @param password xx
     * @return 路径
     */
    @PostMapping("/login")
    public RestObject login(@RequestBody()LoginVo loginVo) {
    	if(StringUtils.isBlank(loginVo.getUsername())) {
            return RestObject.newParamsError("username不能为空！");
    	}
    	if(StringUtils.isBlank(loginVo.getPassword())) {
    		 return RestObject.newParamsError("password不能为空！");
    	}
    	SysUser user = null;
    	try {
			user = userService.login(loginVo.getUsername(), loginVo.getPassword());
		} catch (SystemException e) {
			logger.info(e.getMessage());
            return RestObject.newError(e.getMessage());
		}
    	return RestObject.newOk(user);
    }
    
    /**
     * 退出
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/logout")
    public RestObject logout(HttpServletRequest request, HttpServletResponse response) {
    	userService.logout();
        return RestObject.newOk("退出登录成功");
    }
    
}
